package com.da.lon.wang.xlg.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.CouponAdapter;
import com.da.lon.wang.xlg.bean.CouponBean;
import com.da.lon.wang.xlg.bean.CouponBean.couponlist;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.http.HttpLoader.OnWebLoadListener;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

import java.util.ArrayList;

public class Coupon1Fragment extends BaseFragment {

    private Context context = null;
    private String user_id = null;
    private String deviceId = null;
    private String user_key = null;
    private String ids = null;
    private String data_store = null;
    private int resultCode = 2;

    private ListView couponListView;
    private CouponAdapter adapter;
    private ArrayList<couponlist> datas;
    private View view;

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("Coupon1Fragment");
        MobclickAgent.onResume(getActivity());
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("Coupon1Fragment");
        MobclickAgent.onPause(getActivity());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected View inflateView(LayoutInflater inflater, ViewGroup var2) {
        // TODO Auto-generated method stub
        view = inflater.inflate(R.layout.ouka_coupon1_fragment, null);
        return view;
    }

    @Override
    protected void initView() {
        // TODO Auto-generated method stub
        context = getActivity();

        user_id = PreferenceUtils.getInstance().getSettingStr(
                PreferencesConfig.USER_ID, "");
        deviceId = PreferenceUtils.getInstance().getSettingStr(
                PreferencesConfig.DEVICE_ID, "");
        user_key = PreferenceUtils.getInstance().getSettingStr(
                PreferencesConfig.USER_KEY, "");
        ids = getArguments().getString("ids");
        data_store = getArguments().getString("data_store");

        datas = new ArrayList<couponlist>();
        couponListView = (ListView) view.findViewById(R.id.coupon_lv);

        adapter = new CouponAdapter(context, datas);
        couponListView.setAdapter(adapter);

        couponListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                String couponId = datas.get(position).getId();

                Intent intent = new Intent();

                intent.putExtra("couponId", couponId);
                getActivity().setResult(resultCode, intent);
                getActivity().finish();

            }
        });

        getDatas();

    }

    private void getDatas() {

        String url = XLGConstants.URL + "Cart-coupnlist";

        HttpLoader<CouponBean> loader = new HttpLoader<CouponBean>(context,
                url, CouponBean.class);
        AjaxParams params = new AjaxParams();
        params.put(
                "token",
                XLGDataUtils.encrypt2MD5("Cart" + XLGConstants.SECRET
                        + "coupnlist"));
        params.put("deviceId", deviceId);
        params.put("version", "21");
        params.put("ids", ids);
        if (data_store != null && !"".equals(data_store)) {
            params.put("data_store", data_store);
        }
        params.put("id", user_id);
        params.put("user_key", user_key);
        loader.loadData(params);

        loader.setManagerListener(new OnWebLoadListener<CouponBean>() {

            @Override
            public void OnStart() {
                showDialog();
            }

            @Override
            public void OnError(String error) {
                dismissDialog();
            }

            @Override
            public void onSuccess(CouponBean t) {
                dismissDialog();
                if (t != null && "000000".equals(t.getCode())) {
                    try {
                        if (t.getResult() != null
                                && t.getResult().getCouponlist() != null
                                && datas != null && adapter != null) {
                            datas.clear();
                            datas.addAll(t.getResult().getCouponlist());
                            adapter.notifyDataSetChanged();
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    @Override
    protected void RefreshData() {
        // TODO Auto-generated method stub

    }

}
